package gui;

import java.io.IOException;
import java.net.BindException;

import player.Player;
import gameBasicElements.*;
import gameEngine.Game;
import rulesControl.*;

/**
 * @author Wojciech Łopata
 *
 */
public class LocalGameSettingsPanel extends javax.swing.JPanel {
	private static final long serialVersionUID = -8832321474718980164L;
	private final MainFrame mainFrame;
	
	/** Creates new form GameSettingsPanel */
    public LocalGameSettingsPanel(MainFrame mainFrame)
    {
    	this.mainFrame = mainFrame;
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        approversSelectionPanel1 = new gui.ApproversSelectionPanel();
        boardSelectionPanel1 = new gui.BoardSelectionPanel();
        byoYomiSelectionPanel1 = new gui.ByoYomiSelectionPanel();
        handicapSelectionPanel1 = new gui.HandicapSelectionPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        playerSelectionPanel2 = new gui.PlayerSelectionPanel();
        playerSelectionPanel1 = new gui.PlayerSelectionPanel();
        jButton1 = new javax.swing.JButton();

        jLabel1.setText("Gracz 1 [czarne]");

        jLabel2.setText("Gracz 2 [białe]");

        jButton1.setText("Start");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(boardSelectionPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(handicapSelectionPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(approversSelectionPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(byoYomiSelectionPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2)
                    .addComponent(jLabel1))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(playerSelectionPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 235, Short.MAX_VALUE)
                        .addComponent(jButton1))
                    .addComponent(playerSelectionPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(boardSelectionPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(handicapSelectionPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(byoYomiSelectionPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(approversSelectionPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(38, 38, 38)
                        .addComponent(jLabel1)
                        .addGap(26, 26, 26)
                        .addComponent(jLabel2))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(30, 30, 30)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jButton1)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(playerSelectionPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(playerSelectionPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt)
    {
    	Player player1, player2;
    	
    	if((player1 = playerGetter(playerSelectionPanel1)) == null) return;
    	if((player2 = playerGetter(playerSelectionPanel2)) == null) return;
    	
        ByoYomi byoYomi = byoYomiSelectionPanel1.getCustomByoYomi();
        Handicap handicap = handicapSelectionPanel1.getSelectedHandicap();
        Board board = boardSelectionPanel1.getChoosenBoard();
        MovementApprovers [] approvers = approversSelectionPanel1.getSelectedApprovers();
        
        Rules customRules = new Rules(board, approvers, handicap, byoYomi);
        
        Game game = new Game(customRules, player1, player2);
        mainFrame.startLocalGame(game);
    }
    
    private Player playerGetter(PlayerSelectionPanel panel)
    {
    	try
		{
			return panel.getPlayer(mainFrame.getBoardDrawer().getMovementSource());
		}
    	catch (NumberFormatException e)
		{
			mainFrame.showMessage("Wpisz port dla gracza zdalnego");
		}
    	catch (BindException e)
    	{
    		mainFrame.showMessage("Wybrany port jest już w użyciu");
    	}
    	catch (IllegalArgumentException e)
    	{
    		mainFrame.showMessage("Wybrany port musi mieścić się w przedziale 0 - 65535");
    	}
    	catch (IOException e)
		{
			mainFrame.showMessage("Nie udało się otworzyć połączenia");
		}
		return null;
    }
    
    

    // Variables declaration - do not modify
    private gui.ApproversSelectionPanel approversSelectionPanel1;
    private gui.BoardSelectionPanel boardSelectionPanel1;
    private gui.ByoYomiSelectionPanel byoYomiSelectionPanel1;
    private gui.HandicapSelectionPanel handicapSelectionPanel1;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private gui.PlayerSelectionPanel playerSelectionPanel2;
    private gui.PlayerSelectionPanel playerSelectionPanel1;
    // End of variables declaration

}
